<?php

namespace JsonLowCode\UI;


use JsonLowCode\Helps\Random;
use JsonLowCode\UI;

class Block extends Page
{
    protected string $componentName = 'Block';

    public function __construct(mixed $children = null)
    {
        parent::__construct($children);
//        $blocksData = [
//            "label"=> $blockName,
//            "content"=>  $blocksContent
//        ];
//        UI::block()
//        $this->setAttribute("fileName",$blockName);
        $this->setAttribute("schema",[
            "properties"=>[],
            "events"=>[],
        ]);
        $this->setCacheKey();
    }


    /**
     * @param string $propName
     * @param string $propType
     * @param string $defaultValue
     * @param array $extends
     * @param int $propertiesKey
     * @return $this
     */
    public function defineProp(string $propName, string $propType = "String", string $defaultValue = "", array $extends = [], int $propertiesKey = 0){
        $content = array_merge([
            'property'=>$propName,
            'type'=>$propType,
            'defaultValue'=>$defaultValue
        ], $extends);
        $schema = $this->getAttribute("schema");
        if (isset($schema['properties'][$propertiesKey])){
            $contents = $schema['properties'][$propertiesKey]['content'];
        }else{
            $contents = [];
            $propertiesKey = $schema['properties']?count($schema['properties'])-1:0;
        }
        $contents[] = $content;
        $properties = [
            'content'=>$contents
        ];
        $schema['properties'][$propertiesKey] = $properties;


//        $schema['properties'] = array_merge($schema['properties'], $properties);
//        $schema['properties'] = array_merge($schema['properties'], $properties);
//        {
//            "property": "isEdit",
//            "type": "Boolean",
//            "defaultValue": true,
//            "label": {
//              "text": {
//                "zh_CN": "是否为编辑态"
//              }
//            },
//            "description": {
//            "zh_CN": "是否为编辑态，是则可进行配置报价编辑，否则是查看配置报价详情"
//            },
//            "widget": {
//            "component": "MetaSwitch",
//              "props": {}
//            },
//            "cols": 12,
//            "rules": [],
//            "handle": {}
//          }
        $this->setAttribute("schema", $schema);
        return $this;
    }

    /**
     * @param array $properties
     * @return $this
     */
    public function setSchemaProperties(array $properties): static
    {
        $schema = $this->getAttribute("schema");
        $schema['properties'] = array_merge($schema['properties'], $properties);
        $this->setAttribute("schema", $schema);
        return $this;
    }

    /**
     * @param array $events
     * @return $this
     */
    public function setSchemaEvents(array $events): static
    {
        $schema = $this->getAttribute("schema");
        $schema['events'] = array_merge($schema['events'], $events);
        $this->setAttribute("events", $schema);
        return $this;
    }

    public function schema(){
//        properties [[]]
//        events []
    }
}
